package base;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author yingzhi zhu
 * date 2018/1/10.
 * 总结：创建线程时 需要同时创建它需要运行的方法 thread.run 代表直接在当前的线程执行方法 thread.start代表新建一个线程执行方法
 */
public class Client1 {
    public static void main(String[] args) {
        suspendThread();
    }

    /**
     * 创建线程时 需要同时创建它需要运行的方法 thread.run 代表直接在当前的线程执行方法 thread.start代表新建一个线程执行方法
     */
    private static void createThread() {
        Thread thread = new Thread("t1") {
            @Override
            public void run() {
                System.out.println("线程名：" + Thread.currentThread().getName());
            }
        };
        thread.run();
        thread.start();

        Thread thread1 = new Thread(() -> {
            System.out.println("线程名1：" + Thread.currentThread().getName());
        }, "t2");
        thread1.run();
        thread1.start();
    }

    /**
     * 线程中断
     */
    private static void stopThread() {
        //优雅地中断线程 通过对标志位的设置 中断
        Thread thread = new Thread("t") {
            @Override
            public void run() {
                while (true) {
                    if (Thread.currentThread().isInterrupted()) {
                        System.out.println(Thread.currentThread().getName() + ":线程中断了");
                        break;
                    }
                    Thread.yield();
                }
            }
        };
        thread.start();
        thread.interrupt();


        //当线程柱塞之后 外部调用的interrupt方法 抛出异常 然后可以对这异常进行处理 让线程中断
        Thread thread1 = new Thread("t1") {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        System.out.println("t1 正在睡眠之中");
                        Thread.currentThread().interrupt();
                    }
                    if (Thread.currentThread().isInterrupted()) {
                        System.out.println(Thread.currentThread().getName() + ":线程中断了");
                        break;
                    }

                    Thread.yield();
                }
            }
        };
        thread1.start();
        thread1.interrupt();

    }


    static Object object = new Object();
    static Object object1 = new Object();

    /**
     * 线程挂起
     */
    public static void suspendThread() {
        Thread thread1 = new Thread("t1") {
            @Override
            public void run() {
                synchronized (object) {
                    System.out.println("正在使用锁" + getName());
                    Thread.currentThread().suspend();
                }
            }
        };

        Thread thread2 = new Thread("t2") {
            @Override
            public void run() {
//                synchronized (object1){
                System.out.println("正在使用锁" + getName());
                Thread.currentThread().suspend();
//                }
            }
        };
        try {
            thread1.start();
            Thread.sleep(1000);
            thread2.start();
            Thread.sleep(1000);
            thread1.resume();
            thread2.resume();
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
